﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RequirementAndBug.Common.Entities.UserModels;
using RequirementAndBug.Common;
using RequirementAndBug.Web.Models;

namespace RequirementAndBug.Web.Controllers
{
    public class UserController : BaseController
    {
        [AuthenticationAttribute(IsUsedAttribute = false)]
        public ActionResult Login()
        {
            return View();        
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AuthenticationAttribute(IsUsedAttribute = false)]
        public ActionResult Login(UserLogin login)
        {
            if (ModelState.IsValid)
            {                
                UserInfo userInfo;
                int x = userService.CheckUserLogin(login.UserCode,Utils.GetMD5( login.Password),out userInfo);
                if (x > 0)
                {
                    Session["UserID"] = userInfo.UserID;
                    Session["UserName"] = userInfo.NickName;
                    Session["UserCode"] = userInfo.UserCode;
                    Session["RoleID"] = userInfo.RoleID;
                    return RedirectToAction("Index", "Bug");
                }
                else
                {
                    ModelState.AddModelError("Error_PwdModify", "对不起，请重新登陆");
                }
            }
            return View();
        }

        protected override void Dispose(bool disposing)
        {        
            base.Dispose(disposing);
        }

        /*
        //
        // GET: /User/

        public ActionResult Index()
        {
            //db.Database.CreateIfNotExists();
            return View(db.REQUsers.ToList());
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(int id = 0)
        {
            REQUser requser = db.REQUsers.Find(id);
            if (requser == null)
            {
                return HttpNotFound();
            }
            return View(requser);
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /User/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(REQUser requser)
        {
            if (ModelState.IsValid)
            {
                db.REQUsers.Add(requser);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(requser);
        }

        //
        // GET: /User/Edit/5

        public ActionResult Edit(int id = 0)
        {
            REQUser requser = db.REQUsers.Find(id);
            if (requser == null)
            {
                return HttpNotFound();
            }
            return View(requser);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(REQUser requser)
        {
            if (ModelState.IsValid)
            {
                db.Entry(requser).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(requser);
        }

        //
        // GET: /User/Delete/5

        public ActionResult Delete(int id = 0)
        {
            REQUser requser = db.REQUsers.Find(id);
            if (requser == null)
            {
                return HttpNotFound();
            }
            return View(requser);
        }

        //
        // POST: /User/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            REQUser requser = db.REQUsers.Find(id);
            db.REQUsers.Remove(requser);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        */
        
    }
}